package com.stu.manager.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.user.entity.SysUser;
import com.stu.manager.user.entity.dto.RegistDto;
import com.stu.manager.user.entity.dto.UserDto;
import com.stu.manager.user.entity.dto.UserLoginDto;
import com.stu.manager.user.service.SysUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
@CrossOrigin
@Slf4j
@Api(value = "登录验证")
public class SysAdminLoginController {

    @Autowired
    public SysUserServiceImpl userServiceImpl;

    //login
    @PostMapping("login")
    public Result login(@RequestBody UserLoginDto userLoginDto) {
        Result result = userServiceImpl.verifyUser(userLoginDto);
        return result;
    }

    //info
    @GetMapping("info")
    public Result info() {
        return Result.ok().data("roles", "[admin]").data("name", "admin").data("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }

    //登出
    @PostMapping("logout")
    public Result logout() {
        return Result.ok();
    }


    @ApiOperation("教师注册")
    @PostMapping("regist")
    public Result registUser(@RequestBody RegistDto registDto) {
        Result result = userServiceImpl.registUser(registDto);
        return result;
    }

    @ApiOperation("查询所有用户")
    @PostMapping("findAllUser")
    public Result findAllUser() {
        userServiceImpl.findAllUser();

        return Result.ok();
    }

    @ApiOperation("模糊条件查询加分页")
    @PostMapping("pageUser/{current}/{limit}")
    public Result pageUser(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) UserDto userDto, HttpServletRequest request) {
        String header = request.getHeader("X-Token");
        if(!header.equals("00000")){
            return Result.ok().message("您无此权限操作");
        }


        Page<SysUser> pageUser = new Page<>(current, limit);
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();

        String teacherName = userDto.getTeacherName();
        String jobNum = userDto.getJobNum();
        if (!StringUtils.isEmpty(teacherName)) {
            wrapper.like("teacher_name", teacherName);
        }
        if (!StringUtils.isEmpty(jobNum)) {
            wrapper.like("job_num", jobNum);
        }

        IPage<SysUser> sysUserIPage = userServiceImpl.pageUser(pageUser, wrapper);
        List<SysUser> records = sysUserIPage.getRecords();
        long total = sysUserIPage.getTotal();

        return Result.ok().data("total", total).data("records", records);
    }


    @ApiOperation("查询用户密码")
    @GetMapping("selectUserByJobNum/{jobNum}")
    public Result selectUserByJobNum(@PathVariable String jobNum) {
        SysUser sysUser = userServiceImpl.selectUserByJobNum(jobNum);
        return Result.ok().data("item", sysUser);
    }


    @ApiOperation("修改用户密码")
    @PostMapping("editUser")
    public Result editUser(@RequestBody SysUser sysUser) {
        Result result = userServiceImpl.editUser(sysUser);
        return  result;
    }

    @ApiOperation("删除用户")
    @DeleteMapping("delUser/{jobNum}")
    public Result delUser(@PathVariable String jobNum) {
        Result result = userServiceImpl.delUser(jobNum);
        return result;
    }

}
